package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.CustomerBalance;
import com.dianxin.farm.entity.CustomerInfo;
import com.dianxin.farm.entity.WithdrawBill;
import com.dianxin.farm.entity.pojo.WithdrawPageResponse;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.mapper.bill.WithdrawBillMapper;
import com.dianxin.farm.mapper.customer.CustomerBalanceMapper;
import com.dianxin.farm.request.common.IdRequest;
import com.dianxin.farm.request.common.PageRequest;
import com.dianxin.farm.request.withdraw.WithdrawPageRequest;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.util.exception.UserOperationException;
import com.dianxin.farm.util.order.Generator;
import com.dianxin.farm.util.order.IdGeneratorFactory;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 橱窗信息表 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/withdrawBill", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@AuthorCheck
@Api(description = "提现表单控制器")
public class WithdrawController {

    @Resource
    private CurrentLoginService currentLoginService;
    @Resource
    private CustomerBalanceMapper customerBalanceMapper;
    @Resource
    private WithdrawBillMapper withdrawBillMapper;

    @ApiOperation("结算提现")
    @RequestMapping(value = "/agree")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse agree(@Valid @RequestBody IdRequest request) {
        Integer id=request.getId();
        WithdrawBill bill=withdrawBillMapper.selectById(id);
        if (bill!=null && bill.getStatus()!=1){
            bill.setStatus(1);
            withdrawBillMapper.updateById(bill);

            Integer customerId=bill.getCustomerId();
            BigDecimal money=bill.getMoney();
            money=money.multiply(new BigDecimal("-1"));
            CustomerBalance customerBalance=customerBalanceMapper.selectForUpdate(customerId);
            customerBalanceMapper.addBalance(customerBalance.getId(),money);

        }

        return ApiResponseUtils.success();
    }

    @ApiOperation("删除无效提现")
    @RequestMapping(value = "/delete")
    @ResponseBody
    @AuthorCheck
    public ApiResponse delete(@Valid @RequestBody IdRequest request) {
        WithdrawBill bill=withdrawBillMapper.selectById(request.getId());
        if (bill!=null){
            Integer status=bill.getStatus();
            if (status==1){
                throw new UserOperationException("不能删除已结算提现记录。");
            }
            withdrawBillMapper.deleteById(request.getId());
        }

        return ApiResponseUtils.success();
    }

    @ApiOperation("拒绝提现")
    @RequestMapping(value = "/refuse")
    @ResponseBody
    @AuthorCheck
    public ApiResponse refuse(@Valid @RequestBody IdRequest request) {

        WithdrawBill bill=withdrawBillMapper.selectById(request.getId());
        if (bill!=null && bill.getStatus()!=1){
            bill.setStatus(2);
            withdrawBillMapper.updateById(bill);
        }

        return ApiResponseUtils.success();
    }


    @ApiOperation("提现列表")
    @RequestMapping(value = "/withdrawPage")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse<PageInfo<WithdrawPageResponse>> withdrawPage(@Valid @RequestBody WithdrawPageRequest request) {
        PageHelper.startPage(request.getPage(),request.getSize());

        List<WithdrawPageResponse> withdrawBillList=withdrawBillMapper.selectByCondition(null,request.getCustomerName(),request.getPhone(),request.getBillCode(),request.getStatus());
        return ApiResponseUtils.success(new PageInfo<>(withdrawBillList));

    }



}
